package org.appmon.core.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.appmon.core.config.BehaviourProperties;
import org.appmon.core.config.PackageBaseInfo;
import org.appmon.core.config.XmlReader;

public class CountViewServlet extends HttpServlet{
	
	/*
	 * This Servlet is for Temporary view of the hit count values.
	 * 
	 */
			
	private static final long serialVersionUID = -6192494816408060036L;

	public void init() throws ServletException
	  {
		
	  }
	
	public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
		{
		// Set response content type
			response.setContentType("text/html");
			
			StringBuilder outputString = new StringBuilder();
			outputString.append("<table width=\"95%\">");			
			outputString.append("<tr><th>Package-Class_Method</th><th>Count</th></tr>");			
			
			Map classToInclude = XmlReader.applist;//PackageBaseInfo.getInstance().getClassToInclude();
			System.out.println("view servlet map : "+classToInclude);
			//Iterator iterator = classToInclude.keySet().iterator();
			Iterator iterator=XmlReader.applist.keySet().iterator();
			Class noparams[] = {};
			while (iterator.hasNext()) {
				String key=(String) iterator.next();
				BehaviourProperties behaviourProperties = (BehaviourProperties) classToInclude.get(key);
				
				if(classToInclude.containsKey(key) && behaviourProperties.isMethodCountable()){
										
					try {
						Class cls = Class.forName(key);
						Object obj = cls.newInstance();
						
						Method method = cls.getDeclaredMethod("getAllCount", noparams);
						String returnValue = (String) method.invoke(obj, null);
//						System.out.println("Return : "+returnValue);
						String metArr[] = returnValue.split(":");
//						for(int i=0;i<metArr.length;i++){
//							String inArr[]=metArr[i].split("~");
//							System.out.println("IN : "+inArr[0]);
//							System.out.println("IN : "+inArr[1]);
//						}
						
						for(int i=0;i<metArr.length;i++){
							String inArr[]=metArr[i].split("~");
							outputString.append("<tr>");
							outputString.append("<td align=\"left\">");
							outputString.append(inArr[0]);							
							outputString.append("</td>");
							outputString.append("<td align=\"left\">");							
							outputString.append(inArr[1]);
							outputString.append("</td>");
							outputString.append("</tr>");
						}
						cls=null;
						obj=null;
						method=null;
					} catch (Exception e) {
						e.printStackTrace();
					} 			
					
				}
			
			}			
			outputString.append("</table>");			
			System.out.println("Output String : "+outputString.toString());
		PrintWriter out = response.getWriter();
		out.println(outputString.toString());
		}

		public void destroy()
		{
		// do nothing.
		}


}
